//
//  RootViewController.m
//  UICollectionVIewDemo
//
//  Created by 曾墨 on 16/10/25.
//  Copyright © 2016年 YuanJiang. All rights reserved.
//

#import "RootViewController.h"

#import "customCell.h"
#import "HeaderView.h"
#import "FooterView.h"
@interface RootViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>//遵循代理,其中UICollectionViewDelegateFlowLayout 不用设置代理,因为.系统默认已经设置过
//@property(nonatomic,strong)  UICollectionView *collectionView;

@end

@implementation RootViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title =@"集合视图";
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationController.navigationBar.titleTextAttributes=@{NSForegroundColorAttributeName:[UIColor whiteColor]};
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(80, 80);
    layout.sectionInset= UIEdgeInsetsMake(5, 5, 0, 5);
    layout.minimumLineSpacing=10;
    layout.minimumInteritemSpacing =5;
    layout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    
    
    UICollectionView *collectionView= [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    [self.view addSubview:collectionView];
    collectionView.delegate=self;
    collectionView.dataSource=self;
    
    //区头区尾
    layout.headerReferenceSize =CGSizeMake(0, 30);
    layout.footerReferenceSize =CGSizeMake(0, 40);
    
    //背景
    collectionView.backgroundColor= [UIColor whiteColor];
    
    //注册
    [collectionView registerClass:[customCell class] forCellWithReuseIdentifier:@"cell"];
    [collectionView registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [collectionView registerClass:[FooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    
    
    
}

-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 4;
}

-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    customCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.titleLabel.text= [NSString stringWithFormat:@"%ld区%ld行",indexPath.section,indexPath.row];
    cell.backgroundColor= [UIColor lightGrayColor];
    return cell;
    
    
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind== UICollectionElementKindSectionHeader) {
        HeaderView *header= [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        if (indexPath.section==0) {
            header.headerLabel.text= [NSString stringWithFormat:@"芳芳我爱你"];
        }else{
            header.headerLabel.text= [NSString stringWithFormat:@"芳芳是个大美女"];
        }
        return header;
    }else{
        FooterView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        return footer;

    }
    
}
//点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSLog(@"当前点击的是第%ld个section\n第%ld个row",indexPath.section,indexPath.row);
    
}

////返回item的高度
//-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.section == 0) {
//        return CGSizeMake(100, 150);
//    }else{
//        return CGSizeMake(100, 100);
//    }
//}
////返回页眉的高度
//-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
//    if (section == 0) {
//        return CGSizeMake(375, 80);
//    }else{
//        return CGSizeMake(375, 30);
//    }
//}




































@end
